/**
 * @description 请求封装、上传图片封装
 */

import config from '@/config/index.js';
import { getAddress, getLocale } from '@/config/storage.js'
const {
	baseUrl,
	tokenTypeName,
	tokenKey,
	address
} = config

import {
	getToken,
	removeToken,
	removeAddress,
	removeActive
} from '@/config/storage.js'


// n秒后接口无响应，开始loading
var waitTime = 800

const httpRequest = (url, data, isencryption=true, method = 'POST') => {
	
	let lang = getLocale()||'zh-Hans'
	if(lang=='zh-Hans')lang = 'zh-cn'
	else lang = 'en-us'
	
	let httpDefaultOpts = {
		url: baseUrl + url,
		// 默认POST
		method,
		header: {
			[tokenKey]: tokenTypeName + getToken()||'',
			// [tokenKey]: tokenTypeName +'c2d2c53e442a927059c45ddff4b680e6'||'',
			// "Custom-header": lang,
			// [address]: getAddress()||'',
			// JSON传参
			// "Content-Type": "application/json; charset=UTF-8",
			// formData传参
			"Content-Type": "application/x-www-form-urlencoded",
			// "X-Requested-With": "XMLHttpRequest"
		},
		dataType: 'json',
	}
	if (data) {
		httpDefaultOpts = {
			...httpDefaultOpts,
			...{
				data
			}
		}
	}
	let promise = new Promise((resolve, reject) => {
		// loading定时器
		let timer = setTimeout(() => {
			uni.showLoading({
				mask: true
			})
		}, waitTime)

		uni.request({
			...httpDefaultOpts,
			success: res => {
				if(res.statusCode>=200&&res.statusCode<300){
					if(res.data.code==1){
						resolve(res.data.data)
					}else if(res.data.code==401){
						removeAddress()
						removeToken()
						removeActive()
						uni.reLaunch({
							url:"/pages/index"
						})
					}else{
						uni.showToast({
							title:res.data.info,
							icon:"none"
						})
						reject()
					}
				}else{
					reject()
				}
			},
			complete: () => {
				// 清除定时器和loading
				uni.hideLoading()
				clearTimeout(timer)
				timer = null
			}
		})
	})
	return promise
};

const upload = (url, file, name="file") => {
	return new Promise((resolve, reject) => {
		let timer = setTimeout(() => {
			uni.showLoading({
				mask: true
			})
		}, waitTime)
		uni.uploadFile({
			url: baseUrl + url,
			filePath: file,
			name,
			header: {
				[tokenKey]: tokenTypeName + getToken()
			},
			success: res => {
				let data = JSON.parse(res.data)
				if (res.statusCode == 200) {
					if(data.code==1)resolve(data.data)
					else{
						uni.showToast({
							title: data.msg,
							icon: "none"
						})
						reject()
					}
				} else if (res.statusCode == 401) {
					reject()
				} else {
					uni.showToast({
						title: res.errMsg,
						icon: "none"
					})
					reject()
				}
			},
			complete: () => {
				// 清除定时器和loading
				uni.hideLoading()
				clearTimeout(timer)
				timer = null
			}
		})
	})
}

const get = (url, data, isencryption=true) => httpRequest(url, data, isencryption, 'GET')
const post = (url, data, isencryption=true) => httpRequest(url, data, isencryption, 'POST')
const del = (url, data, isencryption=true) => httpRequest(url, data, isencryption, 'DELETE')

export default {
	get,
	post,
	del,
	upload
}